
package com.probiz.estore.common.model.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.probiz.estore.common.model.order.base.OrderShipmentTbl;
import com.probiz.estore.common.model.system.ShippingRate;

/**
 * Model class for OrderShipment. Add not database mapped fileds in this class.
 */
public class OrderShipment extends OrderShipmentTbl {
	public static enum FilterHqlName {SEARCH_4_SHIP,SEARCH_4_RETURN};

	public final static Short	DOCUMENT_TYPE_SHIP	= Short.valueOf((short) 1); // 发货单
	public final static Short	DOCUMENT_TYPE_RETURN= Short.valueOf((short) 2); // 退货单
	

	private List<ShippingRate>	availableShippingRates = new ArrayList<ShippingRate>();

	/**
	 * Default Empty Constructor for class OrderShipment
	 */
	public OrderShipment() {
		super();
	}

	/**
	 * 定义实体的业务名取值； orderShipmentName 必须手工完成这个部分，否则编译不通过。
	 */
	public String getOrderShipmentName() {
		if (orderShipmentId == null)
			return "";
		else
			// 返回一个指定有业务意义的属性值;
			// 如：product的VO就用product.productName
			return this.documentNo;
	}

	/**
	 * Default Key Fields Constructor for class OrderShipment
	 */
	public OrderShipment(Integer in_orderShipmentId) {
		super(in_orderShipmentId);
	}
	
	/**
	 * 获得商品类的发退货明细项
	 * @return
	 */
	public List<OrderShipmentItem> getOrderShipmentItems4Product(){
		List<OrderShipmentItem> orderShipmentItemList = new ArrayList<OrderShipmentItem>();
		for(OrderShipmentItem orderShipmentItem: (Set<OrderShipmentItem>)orderShipmentItems){
			if(OrderSku.ITEM_TYPE_PRODUCT.equals(orderShipmentItem.getOrderSku().getItemType())){
				orderShipmentItemList.add(orderShipmentItem);
			}
		}
		return orderShipmentItemList;
	}
	
	/**
	 * 获得礼券类的发退货明细项
	 * @return
	 */
	public List<OrderShipmentItem> getOrderShipmentItems4Gc(){
		List<OrderShipmentItem> orderShipmentItemList = new ArrayList<OrderShipmentItem>();
		for(OrderShipmentItem orderShipmentItem: (Set<OrderShipmentItem>)orderShipmentItems){
			if(OrderSku.ITEM_TYPE_GC.equals(orderShipmentItem.getOrderSku().getItemType())){
				orderShipmentItemList.add(orderShipmentItem);
			}
		}
		return orderShipmentItemList;
	}
	
	/**
	 * 获得礼品类的发退货明细项
	 * @return
	 */
	public List<OrderShipmentItem> getOrderShipmentItems4Gift(){
		List<OrderShipmentItem> orderShipmentItemList = new ArrayList<OrderShipmentItem>();
		for(OrderShipmentItem orderShipmentItem: (Set<OrderShipmentItem>)orderShipmentItems){
			if(OrderSku.ITEM_TYPE_GIFT.equals(orderShipmentItem.getOrderSku().getItemType())){
				orderShipmentItemList.add(orderShipmentItem);
			}
		}
		return orderShipmentItemList;
	}
	
	/**
	 * 获得发货单内物品数量
	 * @return
	 */
	public Integer getItemCount(){
		Integer itemCount  = 0;
		for(OrderShipmentItem orderShipmentItem: getOrderShipmentItems4Product()){
			itemCount+= orderShipmentItem.getDeliveryQuantity();
		}
		return itemCount;
	}
	
	/**
	 * 是否虚拟发货单---对应的订单或者发退货单中只有礼券或者虚拟商品等
	 * @return
	 */
	public boolean getIsVirtual(){
		if(this.getShippingAddress().getIsVirtual()){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 获得快递100信息，以list方式显示
	 * @return
	 */
	public List<Kuaidi100Info> getKuaidi100Message4List(){
		List<Kuaidi100Info> list = new ArrayList<Kuaidi100Info>();
		if(StringUtils.isNotBlank(this.getKuaidi100Message())){
			try {
				JSONObject jsonObject = new JSONObject(this.getKuaidi100Message());
				String status = jsonObject.getString("status");
				if("1".equals(status)){
					JSONArray data = jsonObject.getJSONArray("data");
					for(int i=0; i< data.length(); i++){
						Kuaidi100Info info = new Kuaidi100Info();
						JSONObject jsonInfo = data.getJSONObject(i);
						info.setTime(jsonInfo.getString("time"));
						info.setContext(jsonInfo.getString("context"));
						list.add(info);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}   
		}
		return list;
	}
	/**
	 * 获得快递100 status信息
	 * @return
	 */
	public String getKuaidi100Message4StatusMessage(){
		if(StringUtils.isNotBlank(this.getKuaidi100Message())){
			try {
				JSONObject jsonObject = new JSONObject(this.getKuaidi100Message());
				String status = jsonObject.getString("status");
				if("0".equals(status)){
					String message = jsonObject.getString("message");
					return message;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}   
		}
		return null;
	}
	
	public class Kuaidi100Info{
		private String time;
		private String context;
		public String getTime() {
			return time;
		}
		public void setTime(String time) {
			this.time = time;
		}
		public String getContext() {
			return context;
		}
		public void setContext(String context) {
			this.context = context;
		}
		
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}
	
	

}
